package cn.tedu.system.live.service.impl;

import cn.tedu.system.base.exception.ServiceException;
import cn.tedu.system.base.response.StatusCode;
import cn.tedu.system.live.mapper.LiveMapper;
import cn.tedu.system.live.pojo.dto.LiveRoomParam;
import cn.tedu.system.live.pojo.entity.LiveRoom;
import cn.tedu.system.live.pojo.vo.LiveRoomVO;
import cn.tedu.system.live.service.LiveService;
import cn.tedu.system.user.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Slf4j
@Service
@Transactional
public class LiveServiceImpl implements LiveService {
    @Autowired
    private LiveMapper liveRoomMapper;

    @Override
    public Long createLiveRoom(LiveRoomParam live, HttpSession session) {
        log.debug("创建直播业务参数:{}", live);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            throw new ServiceException(StatusCode.UNAUTHORIZED);
        }
        LiveRoom liveRoom = new LiveRoom();
        BeanUtils.copyProperties(live, liveRoom);
        liveRoom.setUserId(userVO.getId());
        liveRoom.setLiveStatus(1);
        liveRoom.setCreateTime(new Date());
        liveRoom.setUpdateTime(new Date());

        if (liveRoomMapper.insert(liveRoom)<=0){
            throw new ServiceException("直播创建失败");
        }
        return liveRoom.getId();
    }

    @Override
    public List<LiveRoomVO> getLiveRooms() {
        log.debug("获取直播房间");
        List<LiveRoomVO> rooms = liveRoomMapper.selectAll();
        return rooms;
    }

    @Override
    public LiveRoomVO getLiveRoomById(Long id) {
        log.debug("获取指定id直播房间详情:{}",id);
        return liveRoomMapper.selectById(id);
    }

    @Override
    public void stopLive(Long id) {
        log.debug("停止直播业务:{}", id);
        if(liveRoomMapper.stopLive(id)<=0){
            throw new ServiceException("停止直播失败");
        }
    }

//    @Override
//    public List<LiveRoom> getLiveRoomsByStatus(int status) {
//        log.debug("获取直播业务参数:{}",status);
//        return liveRoomMapper.selectByStatus(status);
//    }
//
//    @Override
//    public List<LiveRoom> getUserLiveRooms(Long userId) {
//        return liveRoomMapper.selectByUserId(userId);
//    }
//
//    @Override
//    @Transactional
//    public void startLive(Long roomId) {
//        liveRoomMapper.updateStatus(roomId, 1);
//    }

//    @Override
//    public String generateStreamKey() {
//        return UUID.randomUUID().toString().replace("-", "");
//    }
}
